package com.game.thrones.controller.webpage;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import com.game.thrones.domain.webpage.WebNewsDO;
import com.game.thrones.service.web.WebEndemicService;
import com.game.thrones.service.web.WebNewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.game.thrones.constant.Errors;
import com.game.thrones.controller.BaseController;
import com.game.thrones.controller.input.DataInput;
import com.game.thrones.controller.input.PageInput;
import com.game.thrones.controller.output.BaseOutput;
import com.game.thrones.controller.output.PageOutput;
import com.game.thrones.controller.output.ResultResponseBeanUtil;
import com.game.thrones.domain.KbmContent;
import com.game.thrones.domain.webpage.WebApplyDO;
import com.game.thrones.domain.webpage.WebCourseDO;
import com.game.thrones.exception.GameGlobalException;
import com.game.thrones.service.web.WebApplyService;
import com.game.thrones.service.web.WebCourseService;


//@Api(value = "网站-报名信息", description = "网站-报名信息-田春阳", produces = "application/json")
@Controller
public class WebApplyController extends BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	WebApplyService webApplyService;
	@Autowired
	WebCourseService webCourseService;
	@Autowired
	WebEndemicService webEndemicService;
	@Autowired
	WebNewsService webNewsService;
//	@ApiOperation(value = "分页列表", notes = "分页列表")
	@ResponseBody
	@RequestMapping(value = "/web/webApply/listByPage", method = RequestMethod.POST)
	public PageOutput<WebApplyOutput> listByPage(HttpServletRequest request, @RequestBody PageInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		input.setKbmContent(kbmContent);
		List<WebApplyOutput> outputList = new ArrayList<WebApplyOutput>();
		List<WebApplyDO> entityList = webApplyService.findByPage(input);
		for(WebApplyDO entity:entityList){
			WebApplyOutput output = new WebApplyOutput();
			BeanUtils.copyProperties(entity, output);
			if(entity.getType()!=null) {
				if (entity.getType() == 1) {
					WebNewsDO webNews = webNewsService.findOneById(kbmContent, entity.getWebCourseId());
					if (webNews != null) {
						output.setWebCourseName(webNews.getTitle());
					}
				} else if (entity.getType() == 2) {
					WebCourseDO webCourse = webCourseService.findOneById(kbmContent, entity.getWebCourseId());
					if (webCourse != null) {
						output.setWebCourseName(webCourse.getName());
					}
				}
			}
			outputList.add(output);
		}
		Integer total = webApplyService.countByPage(input);
		return ResultResponseBeanUtil.pageSuccess(outputList, input.getSize(), input.getPage(), total);
	}

//	@ApiOperation(value = "新增", notes = "新增")
	@ResponseBody
	@RequestMapping(value = "/web/webApply/add", method = RequestMethod.POST)
	public BaseOutput<Void> add(HttpServletRequest request, @Valid @RequestBody WebApplyDO input) {
		KbmContent kbmContent = webEndemicService.findKbmContent(request);
		input.setKbmContent(kbmContent);
		webApplyService.add(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "修改", notes = "修改")
	@ResponseBody
	@RequestMapping(value = "/web/webApply/update", method = RequestMethod.POST)
	public BaseOutput<Void> update(HttpServletRequest request, @Valid @RequestBody WebApplyDO input) {
		KbmContent kbmContent = getCurrentInfo(request);
		if (kbmContent == null) {
			throw new GameGlobalException(Errors.MODIFY_ERROR);
		}
		input.setKbmContent(kbmContent);
		webApplyService.update(input);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "删除", notes = "删除")
	@ResponseBody
	@RequestMapping(value = "/web/webApply/delete", method = RequestMethod.POST)
	public BaseOutput<Void> delete(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebApplyDO entity = webApplyService.findOneById(kbmContent, input.getDataId());
		if (entity == null) {
			throw new GameGlobalException(Errors.DATA_NOTEXIST);
		}
		entity.setKbmContent(kbmContent);
		webApplyService.del(entity);
		return ResultResponseBeanUtil.success();
	}
//	@ApiOperation(value = "根据ID获取", notes = "根据ID获取")
	@ResponseBody
	@RequestMapping(value = "/web/webApply/findOneById", method = RequestMethod.POST)
	public BaseOutput<WebApplyDO> findOneById(HttpServletRequest request, @Valid @RequestBody DataInput input) {
		KbmContent kbmContent = getCurrentInfo(request);
		WebApplyDO entity = webApplyService.findOneById(kbmContent, input.getDataId());
		return ResultResponseBeanUtil.success(entity);
	}

	@RequestMapping(value = "/web/webApply/getExcel.xls", method = RequestMethod.GET)
	public void getVerificationCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		KbmContent kbmContent = getCurrentInfo(request);
		response.setContentType("application/json;charset=UTF-8");//防止数据传递乱码
		//InputStream is = new ByteArrayInputStream(os.toByteArray());
		List<WebApplyDO> entityList = webApplyService.findAll(kbmContent);
		StringBuffer output = new StringBuffer();
		output.append("<table><tr><td>姓名</td><td>电话</td></tr>");
		for(WebApplyDO entity:entityList){
			output.append("<tr>");
			output.append("<td>");
			output.append(entity.getName());
			output.append("</td>");
			output.append("<td>");
			output.append(entity.getNumber());
			output.append("</td>");
			output.append("</tr>");
		}
		output.append("</table>");
		ByteArrayInputStream is = new ByteArrayInputStream(output.toString().getBytes());
		OutputStream out = response.getOutputStream();
		try {
			try {
				byte b[] = new byte[1024];
				int j = 0;
				while ((j = is.read(b)) != -1) {
					out.write(b, 0, j);
				}
				out.flush();
				out.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		is.close();
	}
}
